<script setup lang="ts">
import { reqSkuList, reqOnOrCancelSale, reqSkuInfo } from '@/api/product/sku'
import type { ISkuInfo } from '@/api/product/sku/types'
import type { ISkuItem } from '@/api/product/spu/types'
const pageNo = ref(1)
const pageSize = ref(10)
const skuList = ref<ISkuItem[]>([])
const skuTotal = ref(1)
const skuInfo = ref<ISkuInfo>()
const getHasSku = async () => {
  const result = await reqSkuList(pageNo.value, pageSize.value)
  if (result.code == 200) {
    skuList.value = result.data.records as ISkuItem[]
    skuTotal.value = result.data.total
  }
}
onMounted(() => {
  getHasSku()
})
const OnOrCancelSale = async (skuId: number, status: number) => {
  const result = await reqOnOrCancelSale(skuId, status)
  if (result.code == 200) {
    ElMessage.success('状态修改成功')
    getHasSku()
  } else {
    ElMessage.error('状态修改失败')
  }
}
const getSkuInfo = async (skuId: number) => {
  const result = await reqSkuInfo(skuId)
  if (result.code == 200) {
    skuInfo.value = result.data
  }
}
</script>

<template>
  <div>
    <el-card>
      <el-table :data="skuList" border stripe>
        <el-table-column type="index" label="序号" align="center" width="80px"></el-table-column>
        <el-table-column prop="skuName" label="名称" align="center"></el-table-column>
        <el-table-column prop="skuDesc" label="描述" align="center"></el-table-column>
        <el-table-column label="图片" align="center">
          <template #default="{ row }">
            <el-image :src="row.skuDefaultImg" style="width: 100px; height: 100px"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="重量" align="center"></el-table-column>
        <el-table-column prop="price" label="价格" align="center"></el-table-column>
        <el-table-column label="操作" align="center" fixed="right">
          <template #default="{ row }">
            <div>
              <el-button
                :type="row.isSale == 1 ? 'primary' : 'success'"
                size="small"
                :icon="row.isSale == 1 ? 'Bottom' : 'Top'"
                @click="OnOrCancelSale(row.id, row.isSale)"
              ></el-button>
              <el-button size="small" icon="Edit"></el-button>
            </div>
            <div style="margin-top: 10px">
              <el-button type="info" size="small" icon="View" @click="getSkuInfo(row.id)"></el-button>
              <el-button type="danger" size="small" icon="Delete"></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="prev,pager,next,jumper,->,sizes,total"
        @current-change="getHasSku"
        @size-change="getHasSku"
        :background="true"
        :total="skuTotal"
        style="margin-top: 10px"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<style lang="scss" scoped></style>
